import { merge } from "lodash-es";
import { mapStore } from "../utils/store";
import { $http } from "../boot/http";

export default merge(
  {
    namespaced: true,
    state: {},
    mutations: {},
    getters: {},
    actions: {
      // 发布挂单交易
      async exchange({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "otc.exchange",
            loading: async () => {
              const { data } = await $http.post("/m/otc/api/exchange", params);
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      // 取消挂单交易
      async exchangeCancel({ dispatch }, { id }) {
        return dispatch(
          "toggleLoading",
          {
            key: "otc.exchangeCancel",
            loading: async () => {
              const { data } = await $http.post(
                "/m/otc/api/exchange/" + id + "/cancel"
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      // 发布撮合交易
      async trade({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "otc.trade",
            loading: async () => {
              const { data } = await $http.post("/m/otc/api/trade", params);
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      // 确认支付
      async pay({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "otc.pay",
            loading: async () => {
              const { data } = await $http.post(
                "/m/otc/api/trade/" + params.tranId + "/pay",
                { pay_password: params.pay_password, bank: params.bank }
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      // 确认收款
      async confirm({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "otc.confirm",
            loading: async () => {
              const { data } = await $http.post(
                "/m/otc/api/trade/" + params.tranId + "/confirm",
                { pay_password: params.pay_password }
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      // 获取撮合交易详细
      async tradeDetail({ dispatch }, id) {
        return dispatch(
          "toggleLoading",
          {
            key: "otc.tradeDetail",
            loading: async () => {
              const { data } = await $http.get("/m/otc/api/trade/" + id);
              return data;
            }
          },
          { root: true }
        );
      },
      // 提交申诉
      async tradeAppeal({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "otc.tradeAppeal",
            loading: async () => {
              const { data } = await $http.post(
                "/m/otc/api/trade/" + params.id + "/appeal",
                {
                  reason: params.reason,
                  image_list: params.image_list
                }
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      },
      // 获取申述详情
      async getTradeAppeal({ dispatch }, { id }) {
        return dispatch(
          "toggleLoading",
          {
            key: "otc.gettradeAppeal",
            loading: async () => {
              const { data } = await $http.get(
                "/m/otc/api/trade/" + id + "/appeal"
              );
              return data;
            }
          },
          {
            root: true
          }
        );
      }
    }
  },
  // 获取交易币种列表
  mapStore("coin", {
    url: "/m/otc/api/coin",
    options: {
      actionOptions: { commitType: "data" }
    },
    loadingKey: "otc.loadCoin"
  }),
  // 获取挂单交易列表
  mapStore("exchange", {
    url: "/m/otc/api/exchange",
    options: {
      actionOptions: {
        commitType: "infinite"
      }
    },
    loadingKey: "otc.loadexExchange"
  }),
  // 获取撮合交易列表
  mapStore("trade", {
    url: "/m/otc/api/trade",
    options: {
      actionOptions: {
        commitType: "infinite"
      }
    },
    loadingKey: "otc.loadTrade"
  }),
  // 获取我发布的挂单买交易
  mapStore("exchangeMyBuy", {
    url: "/m/otc/api/exchange/my?type=2",
    options: {
      actionOptions: {
        commitType: "infinite"
      }
    },
    loadingKey: "otc.exchange_my_buy"
  }),
  // 获取我发布的挂单卖交易
  mapStore("exchangeMySell", {
    url: "/m/otc/api/exchange/my?type=1",
    options: {
      actionOptions: {
        commitType: "infinite"
      }
    },
    loadingKey: "otc.exchange_my_sell"
  })
);
